package com.fand.site.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.common.service.CommonService;
import com.fand.dto.PaginationDto;
import com.fand.site.dao.SiteDao;
import com.fand.site.dto.DomainNaDto;
import com.fand.site.dto.SiteDeDto;
import com.fand.site.dto.SiteDto;
import com.google.common.collect.Lists;

/**
 * 站服务
 * 
 * @author yaoHT
 */
public class DefaultSiteService implements SiteService {
	private CommonService commonService;
	
	private SiteDao siteDao;
	
	@Override
	public int deleteDomainNaBy1(final String id) {
		return this.siteDao.deleteDomainNaBy1(id);
	}
	
	@Override
	public int deleteDomainNaByIds(final String ids) {
		return this.siteDao.deleteDomainNaByIds(ids);
	}
	
	@Override
	public int deleteSiteBy1(final String id) {
		return this.siteDao.deleteSiteBy1(id);
	}
	
	@Override
	public int deleteSiteBy1(final String[] ids) {
		return this.siteDao.deleteSiteBy1(ids);
	}
	
	@Override
	public int deleteSiteByIds(final String ids) {
		return this.siteDao.deleteSiteByIds(ids);
	}
	
	@Override
	public int deleteSiteDeBy1(final String entityId) {
		return this.siteDao.deleteSiteDeBy1(entityId);
	}
	
	@Override
	public CommonService getCommonService() {
		return this.commonService;
	}
	
	@Override
	public PaginationDto<Map<String, Object>> paginationDomainNaBy1ForMapList(final DomainNaDto domainNa) {
		if (domainNa.getPaginationNo() < 1) {
			final List<Map<String, Object>> mapList = Lists.newArrayList();
			
			return new PaginationDto<Map<String, Object>>().setDataList(mapList);
		}
		
		return this.siteDao.paginationDomainNaBy1ForMapList(domainNa);
	}
	
	@Override
	public PaginationDto<Map<String, Object>> paginationSiteBy1ForMapList(final SiteDto site) {
		if (site.getPaginationNo() < 1) {
			final List<Map<String, Object>> mapList = Lists.newArrayList();
			
			return new PaginationDto<Map<String, Object>>().setDataList(mapList);
		}
		
		return this.siteDao.paginationSiteBy1ForMapList(site);
	}
	
	@Override
	@CacheEvict(value = SiteService.CACHE_NAME, allEntries = true)
	public void reload() {
	}
	
	@Override
	@Transactional
	public String saveDomainNaBy1ForObject(final DomainNaDto domainNa) {
		return this.siteDao.saveDomainNaBy1ForObject(domainNa);
	}
	
	@Override
	@Transactional
	public String saveSiteBy1ForObject(final SiteDto site) {
		final String id = this.siteDao.saveSiteBy1ForObject(site);
		
		final boolean isDefault = BooleanUtils.toBoolean(site.getDefaultCo());
		
		if (isDefault) {
			this.saveSiteDeBySefDefault(id);
		}
		
		return id;
	}
	
	@Override
	public int saveSiteDeBy1(final SiteDeDto siteDe) {
		return this.siteDao.saveSiteDeBy1(siteDe);
	}
	
	@Override
	@Transactional
	public int saveSiteDeBySefDefault(final String entityId) {
		this.siteDao.deleteSiteDeByNQSiteId(entityId);
		
		final SiteDeDto siteDe = new SiteDeDto();
		
		siteDe.setEntityId(entityId);
		
		return this.saveSiteDeBy1(siteDe);
	}
	
	@Override
	public DomainNaDto selectDomainNaBy1ForMappedObject(final String id) {
		return this.siteDao.selectDomainNaBy1ForMappedObject(id);
	}
	
	@Override
	public int selectDomainNaByIdAndCodeForObject(final String id, final String code) {
		return this.siteDao.selectDomainNaByIdAndCodeForObject(id, code);
	}
	
	@Override
	public SiteDto selectSiteBy1ForMappedObject(final String id) {
		return this.siteDao.selectSiteBy1ForMappedObject(id);
	}
	
	@Override
	public List<SiteDto> selectSiteBy1ForMappedObjectList() {
		return this.siteDao.selectSiteBy1ForMappedObjectList();
	}
	
	@Override
	@Cacheable(value = SiteService.CACHE_NAME)
	public SiteDto selectSiteBy2ForMappedObject(final String domainUrl) {
		return this.siteDao.selectSiteBy2ForMappedObject(domainUrl);
	}
	
	@Override
	@Cacheable(value = { SiteService.CACHE_NAME })
	public SiteDto selectSiteByDefaultForMappedObject() {
		return this.siteDao.selectSiteByDefaultForMappedObject();
	}
	
	@Override
	public int selectSiteByIdAndCodeForObject(final String id, final String code) {
		return this.siteDao.selectSiteByIdAndCodeForObject(id, code);
	}
	
	public void setCommonService(final CommonService commonService) {
		this.commonService = commonService;
	}
	
	public void setSiteDao(final SiteDao siteDao) {
		this.siteDao = siteDao;
	}
	
}